package class06_堆和堆排序.test;

import java.util.Arrays;

public class Code03_HeapSort {

    public static void heapSort(int[] arr){
        if(arr == null || arr.length < 2){
            return;
        }
//        for (int i = 0; i < arr.length; i++) {
//            heapInsert(arr, i);
//        }

        for (int i = arr.length - 1 ; i >= 0; i--) {
            heapify(arr, i, arr.length);
        }
        int headSize = arr.length;
        while (headSize > 0){
            swap(arr, 0, headSize - 1);
            heapify(arr, 0, --headSize);
        }
    }

    private static void heapify(int[] arr, int index, int heapSize) {
        int left = index * 2 + 1;
        while (left < heapSize){
            int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left;
            largest = arr[largest] > arr[index] ? largest : index;
            if(largest == index){
                break;
            }
            swap(arr, largest, index);
            index = largest;
            left = index * 2 + 1;
        }
    }

    private static void heapInsert(int[] arr, int index) {
        while (arr[(index - 1) / 2] < arr[index]){
            swap(arr, (index - 1) / 2, index);
            index = (index - 1) / 2;
        }
    }

    private static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void main(String[] args) {
        int testTimes = 500000;
        int maxValue = 100;
        int maxSize = 100;
        boolean succeed = true;
        for (int i = 0; i < testTimes; i++) {
            int[] arr = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr);
            heapSort(arr);
            comparator(arr2);
            if(!isEqual(arr, arr2)){
                succeed = false;
                printArray(arr);
                printArray(arr2);
            }
        }
        System.out.println(succeed?"Good!!":"Fuck!!");
    }

    private static void printArray(int[] arr) {
        if(arr == null){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    private static boolean isEqual(int[] arr, int[] arr2) {
        if(arr == null && arr2 != null){
            return false;
        }
        if(arr != null && arr2 == null){
            return false;
        }
        if(arr == null && arr2 == null){
            return true;
        }
        if(arr.length != arr2.length){
            return false;
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }

    private static void comparator(int[] arr2) {
        Arrays.sort(arr2);
    }

    private static int[] copyArray(int[] arr) {
        if(arr == null) {
            return new int[0];
        }
        int[] help = new int[arr.length];
        for (int i = 0; i < help.length; i++) {
            help[i] = arr[i];
        }
        return help;
    }

    private static int[] generateRandomArray(int maxSize, int maxValue) {
        int size = (int)(Math.random() * maxSize + 1);
        int[] help = new int[size];
        for (int i = 0; i < size; i++) {
            help[i] = (int)(Math.random() * maxValue + 1);
        }
        return help;
    }
}
